*********************************************************************************
*																				*
*						Campaign Panel Book Do-File Chapter 3 -	Control Variables and Auxilliary Variables							*
*																				*
*********************************************************************************

* Here: Coding of all control variables and auxilliary variables

cd "${path}\Reproducible Do-Files\Chapter 3"


***Three-step political interest variable (2009: wave 2, 2013: wave 1)
gen a_polinttri1 = a_polint2
recode a_polinttri1 (0 .25 = 0) (.5=1) (.75 1=2)

gen b_polinttri1 = b_polint1
recode b_polinttri1 (0 .25 = 0) (.5=1) (.75 1=2)


***party identification yes/no wave 1

fre ?_pidyes?


***party identification partisans

fre ?_pidfull1


***Interest election campaign

foreach year in a {
	foreach num in 2 3 4 5 6 {
		gen `year'_wkampfint`num' = `year'_kp`num'_390
		recode `year'_wkampfint`num' (6/100=.)
		replace `year'_wkampfint`num' = (`year'_wkampfint`num'-1)/4
	}
}

foreach year in b {
	foreach num in 2 3 4 5 6 {
		gen `year'_wkampfint`num' = `year'_kp`num'_390
		recode `year'_wkampfint`num' (6/100=.)
		replace `year'_wkampfint`num' = (`year'_wkampfint`num'-5)/(-4)
	}
}

fre ?_wkampfint?


***External efficacy

local suffix "a e"
foreach year in a {
foreach x of local suffix {
foreach num1 of numlist 1 4 6 7 {
gen  `year'_extefficacy`num1'`x'=.
	replace `year'_extefficacy`num1'`x'= (`year'_kp`num1'_050`x'-1)/4
	replace `year'_extefficacy`num1'`x'=. if `year'_kp`num1'_050`x'>24.4	& `year'_extefficacy`num1'`x' !=.a 			//keine Angabe auf Missing
}
}
}



*Create index: check reliability (Cronbach's Alpha) before
foreach year in a {
foreach num of numlist 1 4 6 7 {
alpha `year'_extefficacy`num'a `year'_extefficacy`num'e
	}
	}
	
foreach year in a {
foreach num of numlist 1 4 6 7 {
	gen `year'_extefficacy`num'=.
		replace `year'_extefficacy`num'=(`year'_extefficacy`num'a + `year'_extefficacy`num'e)/2
		replace  `year'_extefficacy`num'=.a if `year'_extefficacy`num'a==.a | `year'_extefficacy`num'e==.a 
	}
}




local suffix "a e"
foreach year in b {
foreach x of local suffix {
foreach num1 of numlist 1 3 7 {
gen  `year'_extefficacy`num1'`x'=.
	replace `year'_extefficacy`num1'`x'= (`year'_kp`num1'_050`x'-1)/4
	replace `year'_extefficacy`num1'`x'=. if `year'_kp`num1'_050`x'>24.4	& `year'_extefficacy`num1'`x' !=.a 			//keine Angabe auf Missing
}
}
}


*Create index: check reliability (Cronbach's Alpha) before
foreach year in b {
foreach num of numlist 1 3 7 {
alpha `year'_extefficacy`num'a `year'_extefficacy`num'e
	}
	}
	
foreach year in b {
foreach num of numlist 1 3 7 {
	gen `year'_extefficacy`num'=.
		replace `year'_extefficacy`num'=(`year'_extefficacy`num'a + `year'_extefficacy`num'e)/2
		replace  `year'_extefficacy`num'=.a if `year'_extefficacy`num'a==.a | `year'_extefficacy`num'e==.a 
	}
}


fre ?_extefficacy?

***Satisfaction with democracy

fre ?_zufr?


***Alienation

fre ?_entfremda?


***Internal efficacy



local suffix "h k"
foreach year in a {
foreach x of local suffix {
foreach num1 of numlist 1 4 6 7 {
gen  `year'_intefficacy`num1'`x'=.
	replace `year'_intefficacy`num1'`x'= (`year'_kp`num1'_050`x'-1)/4
	replace `year'_intefficacy`num1'`x'=. if `year'_kp`num1'_050`x'>24.4	& `year'_intefficacy`num1'`x' !=.a 			//keine Angabe auf Missing
}
}
}


*Create index: check reliability (Cronbach's Alpha) before
foreach year in a {
foreach num of numlist 1 4 6 7 {
alpha `year'_intefficacy`num'h `year'_intefficacy`num'k
	}
	}
	
foreach year in a {
foreach num of numlist 1 4 6 7 {
	gen `year'_intefficacy`num'=.
		replace `year'_intefficacy`num'=(`year'_intefficacy`num'h + `year'_intefficacy`num'k)/2
		replace  `year'_intefficacy`num'=.a if `year'_intefficacy`num'h==.a | `year'_intefficacy`num'k==.a 
	}
}




local suffix "h k"
foreach year in b {
foreach x of local suffix {
foreach num1 of numlist 1 3 7 {
gen  `year'_intefficacy`num1'`x'=.
	replace `year'_intefficacy`num1'`x'= (`year'_kp`num1'_050`x'-1)/4
	replace `year'_intefficacy`num1'`x'=. if `year'_kp`num1'_050`x'>24.4	& `year'_intefficacy`num1'`x' !=.a 			//keine Angabe auf Missing
}
}
}


*Create index: check reliability (Cronbach's Alpha) before
foreach year in b {
foreach num of numlist 1 3 7 {
alpha `year'_intefficacy`num'h `year'_intefficacy`num'k
	}
	}
	
foreach year in b {
foreach num of numlist 1 3 7 {
	gen `year'_intefficacy`num'=.
		replace `year'_intefficacy`num'=(`year'_intefficacy`num'h + `year'_intefficacy`num'k)/2
		replace  `year'_intefficacy`num'=.a if `year'_intefficacy`num'h==.a | `year'_intefficacy`num'k==.a 
	}
}


fre ?_intefficacy?




***Indifference

fre ?_indiff?



***Perceived issue competency

*2009
foreach welle of numlist 1/7 {
foreach partei of numlist 1 4/7 {
recode a_kp`welle'_850 (999=.) (1000=0) (`partei'=1) ///
	(else=0), gen(a_first`partei'`welle')
recode a_kp`welle'_870 (999=.) (1000=0) (`partei'=1) ///
	(else=0), gen(a_second`partei'`welle')
}
}


tab a_kp7_850 a_first47, m

foreach welle of numlist 1/7 {
foreach partei of numlist 1 4/7 {
gen a_komp`partei'`welle' = 0 if a_first`partei'`welle'==0 & ///
	a_second`partei'`welle'==0 & a_kp`welle'_850!=. & a_kp`welle'_870!=.
replace a_komp`partei'`welle' = 0.33 if a_first`partei'`welle'==0 & ///
	a_second`partei'`welle'==1 & a_kp`welle'_850!=. & a_kp`welle'_870!=.
replace a_komp`partei'`welle' = 0.66 if a_first`partei'`welle'==1 & ///
	a_second`partei'`welle'==0 & a_kp`welle'_850!=. & a_kp`welle'_870!=.
replace a_komp`partei'`welle' = 1 if a_first`partei'`welle'==1 & ///
	a_second`partei'`welle'==1 & a_kp`welle'_850!=. & a_kp`welle'_870!=.
}
}


*2013
foreach welle of numlist 1/7 {
foreach partei of numlist 1 4/7 {
recode b_kp`welle'_850a (999=.) (1000=0) (`partei'=1) ///
	(else=0), gen(b_first`partei'`welle')
recode b_kp`welle'_870a (999=.) (1000=0) (`partei'=1) ///
	(else=0), gen(b_second`partei'`welle')
}
}


tab b_kp7_850a b_first47, m

foreach welle of numlist 1/7 {
foreach partei of numlist 1 4/7 {
gen b_komp`partei'`welle' = 0 if b_first`partei'`welle'==0 & ///
	b_second`partei'`welle'==0 & !missing(b_kp`welle'_850a) & !missing(b_kp`welle'_870a)
replace b_komp`partei'`welle' = 0.33 if b_first`partei'`welle'==0 & ///
	b_second`partei'`welle'==1 & !missing(b_kp`welle'_850a) & !missing(b_kp`welle'_870a)
replace b_komp`partei'`welle' = 0.66 if b_first`partei'`welle'==1 & ///
	b_second`partei'`welle'==0 & !missing(b_kp`welle'_850a) & !missing(b_kp`welle'_870a)
replace b_komp`partei'`welle' = 1 if b_first`partei'`welle'==1 & ///
	b_second`partei'`welle'==1 & !missing(b_kp`welle'_850a) & !missing(b_kp`welle'_870a)
}
}


*Drop auxilliary variables

drop ?_first* ?_second*


***Satisfaction with government


**2009

/*Question posed correctly only in one wave in 2009. In wave 4 there is a coding error, which mistakenly assigned missing values for some respondents.
GESIS decided to delete the variable (the respondents) concerned, since the mistake could not be rectified. 
Since it is plausible to assume that the deleted respondents were deleted at random (missing completely at random),
we decided to extract the variable from a previous version of the dataset and to use it in a smaller sample.
*/


replace lfdn09 = _n+1000000 if lfdn09==.

sort lfdn09

save "${data}\data2009_2013 participants 7 waves.dta", replace

use "${data}\scalometer government 2009.dta", clear

rename lfdn lfdn09

rename kp4_730 a_kp4_730

sort lfdn09

save "${data}\scalometer government 2009.dta", replace


use "${data}\data2009_2013 participants 7 waves.dta", clear

drop a_kp4_730


merge 1:1 lfdn09 using "${data}\scalometer government 2009.dta"

drop if _merge == 2

drop _merge

foreach num of numlist 4 6 {
	gen a_regzufr`num' = (a_kp`num'_730-1)/10
	replace a_regzufr`num' = . if a_regzufr`num'>2
}


**2013

foreach num of numlist 1/7 {
	gen b_regzufr`num' = (b_kp`num'_730-1)/10
}



***Evaluation scales

**Merkel

foreach year in a b {
foreach num1 of numlist 1/7 {
	gen `year'_skal_merkel`num1'=.
		replace `year'_skal_merkel`num1'= (`year'_kp`num1'_650a-1)/10
		replace `year'_skal_merkel`num1'= .a if `year'_skal_merkel`num1'==.
		replace `year'_skal_merkel`num1'=. if `year'_skal_merkel`num1'>=9.6 & `year'_skal_merkel`num1'!=.a
}
}


**Steinmeier/Steinbrück


*Skalometer Steinmeier
foreach num1 of numlist 1/7 {
	gen a_skal_steinm`num1'=.
		replace a_skal_steinm`num1'= (a_kp`num1'_650b-1)/10
		replace a_skal_steinm`num1'= .a if a_skal_steinm`num1'==.
		replace a_skal_steinm`num1'=. if a_skal_steinm`num1'>=9.6 & a_skal_steinm`num1'!=.a
}

foreach num1 of numlist 1/7 {
	gen b_skal_steinb`num1'=.
		replace b_skal_steinb`num1'= (b_kp`num1'_650k-1)/10
		replace b_skal_steinb`num1'= .a if b_skal_steinb`num1'==.
		replace b_skal_steinb`num1'=. if b_skal_steinb`num1'>=9.6 & b_skal_steinb`num1'!=.a
}



**Westerwelle/Brüderle

foreach num1 of numlist 1 3 6 7 {
	gen a_skal_westerw`num1'=.
		replace a_skal_westerw`num1'= (a_kp`num1'_650f-1)/10
		replace a_skal_westerw`num1'= .a if a_skal_westerw`num1'==.
		replace a_skal_westerw`num1'=. if a_skal_westerw`num1'>=9.6 & a_skal_westerw`num1'!=.a
}

foreach num1 of numlist 1/7 {
	gen b_skal_bruederle`num1'=.
		replace b_skal_bruederle`num1'= (b_kp`num1'_650o-1)/10
		replace b_skal_bruederle`num1'= .a if b_skal_bruederle`num1'==.
		replace b_skal_bruederle`num1'=. if b_skal_bruederle`num1'>=9.6 & b_skal_bruederle`num1'!=.a
}




**Trittin

foreach num1 of numlist 1 4 7 {
	gen a_skal_trittin`num1'=.
		replace a_skal_trittin`num1'= (a_kp`num1'_650h-1)/10
		replace a_skal_trittin`num1'= .a if a_skal_trittin`num1'==.
		replace a_skal_trittin`num1'=. if a_skal_trittin`num1'>=9.6 & a_skal_trittin`num1'!=.a
}

foreach num1 of numlist 1/7 {
	gen b_skal_trittin`num1'=.
		replace b_skal_trittin`num1'= (b_kp`num1'_650h-1)/10
		replace b_skal_trittin`num1'= .a if b_skal_trittin`num1'==.
		replace b_skal_trittin`num1'=. if b_skal_trittin`num1'>=9.6 & b_skal_trittin`num1'!=.a
}




**Lafontaine/Gysi

foreach num1 of numlist 1 4 7 {
	gen a_skal_lafontaine`num1'=.
		replace a_skal_lafontaine`num1'= (a_kp`num1'_650i-1)/10
		replace a_skal_lafontaine`num1'= .a if a_skal_lafontaine`num1'==.
		replace a_skal_lafontaine`num1'=. if a_skal_lafontaine`num1'>=9.6 & a_skal_lafontaine`num1'!=.a
}

foreach num1 of numlist 1/7 {
	gen b_skal_gysi`num1'=.
		replace b_skal_gysi`num1'= (b_kp`num1'_650j-1)/10
		replace b_skal_gysi`num1'= .a if b_skal_gysi`num1'==.
		replace b_skal_gysi`num1'=. if b_skal_gysi`num1'>=9.6 & b_skal_gysi`num1'!=.a
}


***Preferred coalitions

***2009 (different operationalisation than in 2013)
foreach welle of numlist 1/4 6/7 {   
   
   // Black-Yellow
   gen a_koasge`welle' = 0 if a_w`welle'==1 
   replace a_koasge`welle' = 1 if a_kp`welle'_920a==1 & a_kp`welle'_920c==1 & ///
      a_kp`welle'_920b==0 & a_kp`welle'_920d==0 & a_kp`welle'_920e==0
	replace a_koasge`welle' = . if a_kp`welle'_920a==.
   
   // Grand Coalition
   gen a_koagk`welle' = 0 if a_w`welle'==1 
   replace a_koagk`welle' = 1 if a_kp`welle'_920a==1 & a_kp`welle'_920b==1 & ///
      a_kp`welle'_920c==0 & a_kp`welle'_920d==0 & a_kp`welle'_920e==0  
   replace a_koagk`welle' = . if a_kp`welle'_920a==.
   
   // Red-Green  
   gen a_koarg`welle' = 0 if a_w`welle'==1 
   replace a_koarg`welle' = 1 if a_kp`welle'_920b==1 & a_kp`welle'_920d==1 & ///
      a_kp`welle'_920a==0 & a_kp`welle'_920c==0 & a_kp`welle'_920e==0  
   replace a_koarg`welle' = . if a_kp`welle'_920a==.
   
   // Red-Red-Green
   gen a_koarrg`welle' = 0 if a_w`welle'==1 
   replace a_koarrg`welle' = 1 if a_kp`welle'_920b==1 & a_kp`welle'_920d==1 & ///
      a_kp`welle'_920e==1 & a_kp`welle'_920a==0 & a_kp`welle'_920c==0  
	replace a_koarrg`welle' = . if a_kp`welle'_920a==.  
}

  
// Wave 5: Queried as coalition evaluation scale (not used because measurement displayed large differences)
*2013


tab1 b_kp2_910a

foreach welle of numlist 2 4 6 7{
      // Koalition: Wünschenswert
      recode b_kp`welle'_910a (-99/-1 = .), gen(b_hkoagk`welle')
      recode b_kp`welle'_910b (-99/-1 = .), gen(b_hkoasge`welle')
      recode b_kp`welle'_910c (-99/-1 = .), gen(b_hkoarg`welle')
      recode b_kp`welle'_910e (-99/-1 = .), gen(b_hkoasgr`welle')
      recode b_kp`welle'_910h (-99/-1 = .), gen(b_hkoarrg`welle')
	recode b_kp`welle'_910f (-99/-1 = .) , gen(b_hkoaamp`welle') //traffic light
}	  



// Create coalition dummies in order to adapt measurement to 2009 measure

* Determine standing. Best evaluation = place 1, missing receives missing standing
foreach welle of numlist 2 4 6 7 {
   rowranks b_hkoasgr`welle' b_hkoagk`welle' b_hkoasge`welle' b_hkoarg`welle' ///
      b_hkoarrg`welle' b_hkoaamp`welle' , gen(b_rkoasgr`welle' b_rkoagk`welle' b_rkoasge`welle' ///
      b_rkoarg`welle' b_rkoarrg`welle' b_rkoaamp`welle') descending highmissing
}

* Save values of assigned standing
foreach welle of numlist 2 4 6 7 {
 rowsort b_hkoasgr`welle' b_hkoagk`welle' b_hkoasge`welle' b_hkoarg`welle' ///
      b_hkoarrg`welle' b_hkoaamp`welle', gen(b_vrang1_`welle' b_vrang2_`welle' ///
		b_vrang3_`welle' b_vrang4_`welle' b_vrang5_`welle' b_vrang6_`welle') descending highmissing
}



list b_hkoa*4 b_rkoa*4 b_vrang*4 in 1/50
 
 
/* 
Rules for coalition dummies:

coalition preference dummy for a specific coalition = 1 if this coalition received
the best evaluation by the respondent + this evaluation lies above the mean, as in
being greater than 6.

If a respondent evaluates all coalitions worse than 6, she does not possess
a coalition preference for any coalition. 

If a repondent assigns her highest value to more than one coalition and this 
value is greater than 6, the respondent receives a 1 for more than one coalition.
Wenn jemand mehr als eine Koalition mit seinem besten Wert bewertet und dieser
*/
 

foreach welle of numlist 2 4 6 7 {
   foreach koal in koasge koagk koarg koarrg {
      gen b_`koal'`welle' = .
	  replace b_`koal'`welle' = 0 if b_r`koal'`welle' > 1 & b_r`koal'`welle'!=.
	  replace b_`koal'`welle' = 1 if b_r`koal'`welle' == 1
	  replace b_`koal'`welle' = 0 if b_r`koal'`welle' == 1 & b_h`koal'`welle'<7
	  replace b_`koal'`welle' = 1 if b_r`koal'`welle'!=1 & /// 
		b_h`koal'`welle'==b_vrang1_`welle' & b_vrang1_`welle'>=7 & b_vrang1_`welle'!=.
}
}


// Control

list b_hkoa*2 b_koa*2 in 1/50

egen b_countkoalitions4 = anycount(b_koa*4), values(1)
replace b_countkoalitions = . if b_hkoasge4==. & b_hkoagk4==. & ///
	b_hkoarg4==. & b_hkoarrg4==.
fre b_countkoalitions4  //few missings
fre b_w4b //passt aber

fre b_hkoa*4

/* 
Average coalition evaluation if a preference for a coalition exists according
to our coding schema. Because: if no preference for a coalition exists, the 
coalition evaluation cannot be 11. Logically impossible. Otherwise mistake in code. 
*/

bysort b_koasge4: sum b_hkoasge4 //if koasge4 = 0, hkoasge mustn't be 11 
bysort b_koarrg4: sum b_hkoarrg4

* Problems with single missings in the row?
list lfdn b_hkoa*4 b_rko*4 b_vrang*4 if b_hkoagk4==. & b_hkoasge4==. & b_hkoarg4!=.
//everything is fine



save "${data}\data2009_2013 participants 7 waves.dta", replace
